package cn.ronghuanet.org.service.impl;

import cn.ronghuanet.base.query.BaseQuery;
import cn.ronghuanet.base.service.impl.BaseServiceImpl;
import cn.ronghuanet.base.util.PageInfo;
import cn.ronghuanet.org.domain.Department;
import cn.ronghuanet.org.mapper.DepartmentMapper;
import cn.ronghuanet.org.query.DepartmentQuery;
import cn.ronghuanet.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper mapper;
    @Transactional
    @Override
    public void insert(Department department) {
        mapper.insert(department);
        // 如果是一级部门 它的path是什么? 本身的id
        // 如果是 二级或者三级部门它的path是什么? 上级path + 自己的id\
        if (Objects.isNull(department.getParent().getId())){
            // 没有上级
            department.setPath("/" + department.getId());
        }else{
            // 有上级
            Department parent = mapper.queryById(department.getParent().getId());
            department.setPath(parent.getPath()+"/" + department.getId());
        }
        mapper.update(department);
    }
    @Transactional
    @Override
    public void update(Department department) {
        if (Objects.isNull(department.getParent().getId())){
            // 没有上级
            department.setPath("/" + department.getId());
        }else{
            // 有上级
            Department parent = mapper.queryById(department.getParent().getId());
            department.setPath(parent.getPath()+"/" + department.getId());
        }
        mapper.update(department);
    }
    @Override
    public List<Department> getParent() {
        return mapper.getParent();
    }
}
